/*
 ***************************************************************************
 *           Copyright © 2009 by konglinglong                              *
 *           konglinglong@gmail.com                                        *
 *           我的主页:https://sites.google.com/site/konglinglong/           *
 ***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************
 */

#include <QApplication>
#include <QAction>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QPixmap>
#include "LongSystemTray.h"
#include "LongMainWindow.h"
#include "LongDialogWindow.h"
#include "LongMain.h"
#include "LongDebug.h"

LongSystemTray::LongSystemTray(LongMainWindow *mainWind, LongDialogWindow *dialogWind, QWidget *parent)
	:QWidget(parent),mainWindPtr(mainWind),dialogWindPtr(dialogWind)
{
	createActions();
	createTrayIcon();
	doConnect();
}

LongSystemTray::~LongSystemTray()
{
	//打印调试信息
	DWHERE();
}

void LongSystemTray::showTray()
{
	trayIcon->show();
}

void LongSystemTray::hideTray()
{
	trayIcon->hide();
}

void LongSystemTray::startFlashTray()
{
	if (!trayFlashTimer.isActive())
		trayFlashTimer.start(600);
}

void LongSystemTray::stopFlashTray()
{
	if (trayFlashTimer.isActive())
		trayFlashTimer.stop();

	trayIcon->setIcon(QPixmap(":/resources/trayIcon/yellowstar.png"));
}

void LongSystemTray::flashTray()
{
	static bool flashFlag = true;
	if(flashFlag)
		trayIcon->setIcon(QPixmap(":/resources/trayIcon/newmsg.png"));
	else
		trayIcon->setIcon(QPixmap(""));
	flashFlag = !flashFlag;
}

void LongSystemTray::onIconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
		case QSystemTrayIcon::DoubleClick:
		{
			if(trayFlashTimer.isActive())
			{
				stopFlashTray();
				dialogWindPtr->showNormal();
				dialogWindPtr->raise();
				dialogWindPtr->activateWindow();
			}
			else
			{
				if(mainWindPtr->isHidden())
				{
					mainWindPtr->showNormal();
					mainWindPtr->raise();
					mainWindPtr->activateWindow();
				}
				else
					mainWindPtr->hide();
			}

		}
			break;
		case QSystemTrayIcon::MiddleClick:
			break;
		default:
			;
    }
}

void LongSystemTray::createActions()
{
    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), mainWindPtr, SLOT(close()));
}

void LongSystemTray::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->setIcon(QPixmap(":/resources/trayIcon/yellowstar.png"));

}

void LongSystemTray::doConnect()
{
    connect(&trayFlashTimer, SIGNAL(timeout()), this, SLOT(flashTray()));
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(onIconActivated(QSystemTrayIcon::ActivationReason)));
}
